import { Window_Selectable } from "@/windows/Window_Selectable.js";
import { DataManager } from "@/managers/DataManager.js";
import { TextManager } from "@/managers/TextManager.js";

export class Window_StoreFiles extends Window_Selectable {
    constructor(width,height) {
        super(width,height,false);
        this.init(width,height);
    }

    init(width,height){
        /**
         * @type {{
         * characters,
         * playtime,
         * timestamp
         * }[]}
         */
        this._list=[];

        this.okHandler=null;
        this.cancelHandler=null;
        super.init(width,height);
    }

    destroy(opts){
        super.destroy(opts);
        this._list.length=0;
        this.okHandler=null;
        this.cancelHandler=null;
    }

    row(){return this.length();}
    viewRow(){return 5;}
    itemHeight(){return this.contentsHeight()/this.viewRow();}
    length() {return DataManager.maxSavefiles();}

    setGlobInfo(globInfo){
        if(globInfo){
            this._list=globInfo;
            this.refresh();
        }
    }

    drawItem(index,x,y,width,height) {
        let item=this._list[index];
        if(item){
            this.resetTextColor();
            this.drawText(`${TextManager.file} ${index+1}`, x, y, 180);
            if (width >= 420) {
                this.drawPartyCharacters(item.characters, x,y+height-4);
            }
            this.drawPlaytime(item.playtime, x, y, width,36);
            this.drawDate(item.date,x,y+36,width,height-36)
        }
    }

    drawPartyCharacters(characters, x, y) {
        if (characters) {
            for(let character of characters){
                this.drawCharacter(character[0], character[1], x, y);
                x+=48;
            }
        }
    }

    drawPlaytime(playtime, x, y, width,height) {
        if (playtime) {
            this.drawText(playtime, x, y, width,height,'right');
        }
    }

    drawDate(date,x,y,width,height){
        if(date){
            this.drawText(date,x,y,width,height,'right');
        }
    }

    setItem(index,item){
        if(index<this.length()){
            this._list[index]=item;
            this.refreshItem(index);
        }
    }

    setHandler(name,callback){
        if(name=='ok'){
            this.okHandler=callback;
        }else if(name=='cancel'){
            this.cancelHandler=callback;
        }
    }

    onResponse(index){
        this.okHandler?.(this._list[index],index);
    }

    onCancel(){
        this.cancelHandler?.();
    }
}